package com.bluevally.microservice.user.main;

import com.bluevally.framework.client.channel.ChannelFactory;
import com.bluevally.framework.client.context.AppContext;
import com.bluevally.grpc.user.UserProto;
import com.bluevally.grpc.user.UserServiceGrpc;
import com.bluevally.microservice.user.bindable.UserServiceImpl;
import io.grpc.Channel;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.IOException;

/**
 * Created by lixiaoming on 2019/1/9.
 */
public class Client {
    private static Log logger = LogFactory.getLog(UserServiceImpl.class);


    public static void main(String[] args) throws IOException, InterruptedException {
       /* DataSource ds = JdbcHelper.create();
        AppContext.addBean("dataSource",ds);
        UserServiceImpl userService = AppContext.proxyBindService(UserServiceImpl.class);*/
       logger.info("client start...");
        Channel channel = null;
        try {
            ChannelFactory factory = ChannelFactory.getInstance();
            factory.init("client.properties");
            channel = factory.buildChannel("user");
        }catch (Exception e){
            //如果连不到注册中心，则channel会返回Null
            logger.warn("###########",e);
            //System.exit(-1);
        }
        logger.info("ready work!");

        UserServiceGrpc.UserServiceBlockingStub blockingStub = UserServiceGrpc.newBlockingStub(channel);
        AppContext.addBean("userServiceBlockingStub",blockingStub);
        AppContext.addBean("channel",channel);

        UserProto.UserRegRequest.Builder builder = UserProto.UserRegRequest.newBuilder();
        builder.setName("aa123456");
        builder.setPasswd("123456");

        logger.info("begin send ...");
        UserProto.UserReponse reply = blockingStub.register(builder.build());
        logger.info("receive :"+reply.getMessage());

    }
}
